package cn.dida.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.dida.dao.StaffDao;
import cn.dida.exception.BizzException;
import cn.dida.model.Pageable;
import cn.dida.model.Staff;
import cn.dida.model.Sysres;
import cn.dida.service.StaffService;
/**
 * 账户管理 接口实现类
 * @author Administrator
 *
 */
@Service
public class StaffServiceImpl implements StaffService {
    @Autowired
    private StaffDao staffDao;

    @Override
    public Staff getStaff(String account, String pwd) {
        return staffDao.get(account, pwd);
    }

    @Override
    public Staff getStaff(String account) {
        return staffDao.get(account);
    }

    @Override
    public Staff getStaff(Integer companyId, Integer staffId) {
        return staffDao.get(companyId, staffId);
    }

    @Override
    public List<Sysres> getStaffRes(Integer companyId, Integer staffId) {
        return staffDao.getStaffRes(companyId, staffId);
    }

    @Override
    public Pageable<Staff> queryStaff(Integer companyId, String keyword, Integer state, int page) {
        return staffDao.query(companyId, keyword, state, page);
    }

    @Transactional
    @Override
    public Boolean insertStaff(Staff staff) {
        if (staffDao.getByAccount(staff.getAccount(),staff.getCompanyId()) != null) {
            throw new BizzException(BizzException.error_code_invalid_params, "该账号已经被注册");
        }
        if (staffDao.getByMobile(staff.getMobile(),staff.getCompanyId()) != null) {
            throw new BizzException(BizzException.error_code_invalid_params, "该手机号已经被注册");
        }
        return staffDao.insert(staff) == 1;
    }

    @Transactional
    @Override
    public Boolean updateStaff(Staff staff) {
        return staffDao.update(staff) == 1;
    }

    @Transactional
    @Override
    public Boolean updateStaffAuth(Integer companyId, Integer staffId, Integer... authIds) {
        staffDao.updateAuth(companyId, staffId, authIds);
        return true;
    }
    @Transactional
    @Override
    public Boolean deleteStaff(Integer companyId, Integer id) {
        return staffDao.delete(companyId, id) == 1;
    }

	@Override
	public List<Staff> queryStaffForExport(Integer companyId, String keyword,
			Integer state) {
		// TODO Auto-generated method stub
		return staffDao.queryForExport(companyId, keyword, state);
	}

	@Override
	public int resetPassword(Integer companyId, Integer id) {
		// TODO Auto-generated method stub
		return staffDao.resetPassword(companyId, id);
	}

	@Override
	public int checkOldPassword(Integer companyId, Integer id,
			String oldPassword) {
		// TODO Auto-generated method stub
		return staffDao.checkOldPassword(companyId, id, oldPassword);
	}
	
	@Override
	public int editPassword(Integer companyId, Integer id, String newPassword) {
		// TODO Auto-generated method stub
		return staffDao.editPassword(companyId, id, newPassword);
	}
}
